package com.cuvretool.view.jpanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

import com.cuvretool.model.detail.ModelGroup;
import com.cuvretool.model.detail.ModelUnit;
import com.cuvretool.view.BaseJPanel;
/**
 * 操作设置区域面板
 * since 2015年1月15日 上午12:44:44
 * @author boy
 */
public class JPanelOperate extends BaseJPanel {

	// ===========================================================
	// Constants
	// ===========================================================
	private static final long serialVersionUID = 1L;
	// ===========================================================
	// Fields
	// ===========================================================
	/**容器*/
	private JScrollPane mContainer;
	/**树结构*/
	private JTree mJTree;
	/**视图树的根节点*/
	private final DefaultMutableTreeNode mRoot = new DefaultMutableTreeNode("设置区面板");
	/**model区定义的群组*/
	private ArrayList<ModelGroup> mGroups;
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	/**
	 * 设置操作面板树结构参照骨架
	 */
	public void setOperateTree(ArrayList<ModelGroup> groups){
		mGroups = groups;
	}
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void init() {
		super.init();
		setLayout(new BorderLayout());
		mJTree = new JTree(mRoot);
		mJTree.setBackground(Color.DARK_GRAY);
		DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) mJTree.getCellRenderer();
		cellRenderer.setBackgroundNonSelectionColor(Color.DARK_GRAY);
		cellRenderer.setTextNonSelectionColor(Color.WHITE);
		mContainer = new JScrollPane(mJTree);
		add(mContainer,BorderLayout.CENTER);
	}

	@Override
	public void run() {
		DefaultTreeModel model = (DefaultTreeModel) mJTree.getModel();
		ArrayList<ModelGroup> deList = new ArrayList<ModelGroup>();
		for (int i = 0; i < mGroups.size(); i++) {
			ModelGroup group = mGroups.get(i);
			ArrayList<ModelUnit> units = group.mGroup;
			if(group.isNewOne){
				DefaultMutableTreeNode node = new DefaultMutableTreeNode(group.mName);
				for (int j = 0; j < units.size(); j++) {
					ModelUnit unit = units.get(j);
					DefaultMutableTreeNode child = new DefaultMutableTreeNode(unit.mName+unit.mPosition.toString());
					model.insertNodeInto(child, node, node.getChildCount());
					unit.isNewOne = false;
				}
				model.insertNodeInto(node, mRoot, i);
				group.isNewOne = false;
			} else if(group.isDelete){
				mRoot.remove(i);
				deList.add(group);
			} else {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) mRoot.getChildAt(i);
				for (int j = 0; j < units.size(); j++) {
					ModelUnit unit = units.get(j);
					if (unit.isNewOne) {
						DefaultMutableTreeNode child = new DefaultMutableTreeNode(unit.mName+unit.mPosition.toString());
						model.insertNodeInto(child, node, node.getChildCount());
						unit.isNewOne = false;
					}
					if (unit.isChange) {
						DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(j);
						child.setUserObject(unit.mName+unit.mPosition.toString());
						unit.isChange = false;
					}
					if (unit.isDelete) {
						DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(j);
						model.removeNodeFromParent(child);
						units.remove(unit);
						j--;
					}
				}
			}
		}
		mGroups.removeAll(deList);
		deList.clear();
		
		this.repaint();
	}

	@Override
	public void over() {
		// TODO Auto-generated method stub
		super.over();
	}

	@Override
	public void resize() {
	}
	
	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
